﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using DevExpress.XtraReports.UI;
using DevExpress.XtraReports.UserDesigner;

namespace BHair.Report
{
    public partial class frmhyczmx : WeifenLuo.WinFormsUI.Docking.DockContent
    {
        public frmhyczmx()
        {
            InitializeComponent();
        }
        string constr = ConfigurationManager.ConnectionStrings["BHair.Properties.Settings.BHairDBConnectionString"].ConnectionString;
        private void button1_Click(object sender, EventArgs e)
        {
            if (txtcardID.Text == string.Empty)
            {
                selecttime();
            }
            else
            {
                selectcardID();
            }
        }
        public DataSet selecttime()
        {
            DataSet ds = new DataSet();
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql = string.Format("SELECT  [dmid] as 卡号 ,[dmoney] as 续费金额,[ddate] as 续费日期 ,[dremark] as 备注 ,case([dmode]) when '0' then '充值' when '1' then '转出' when '2' then '转入' when '3' then '赠送' when '4' then '退卡' end as 方式  ,[czdh] as 单据号 ,[zsje] as 赠送金额 ,[jsr] as 经手人 ,[skzh] as 收款方式 ,[skje]  as 收款金额 FROM [deposit] where ddate>='{0}' and ddate<='{1}'", dtpbegin.Text, dtpend.Text+" 23:59:59");
            SqlDataAdapter sda = new SqlDataAdapter(sql, conn);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            dataGridView1.DataSource = dt;
            string sql1= string.Format("SELECT  [dmid]  ,[dmoney],[ddate]  ,[dremark]  ,[dmode] ,[czdh]  ,[zsje]  ,[jsr]  ,[skzh]  ,[skje]   FROM [deposit] where ddate>='{0}' and ddate<='{1}'", dtpbegin.Text, dtpend.Text + " 23:59:59");
            SqlDataAdapter sda1 = new SqlDataAdapter(sql1,conn);
            sda1.Fill(ds, "deposit");
            conn.Close();
            //会员充值合计
            string hj = string.Format("SELECT  sum([dmoney]) FROM [deposit]  where ddate>='{0}' and ddate<='{1}' and dmode='0'", dtpbegin.Text, dtpend.Text + " 23:59:59");
            SqlConnection conn99 = new SqlConnection(constr);
            conn99.Open();
            SqlCommand comm99 = new SqlCommand(hj,conn99);
            txtczhj.Text = comm99.ExecuteScalar().ToString();
            conn99.Close();
            //会员退卡合计
            string tk = string.Format("SELECT  sum([dmoney]) FROM [deposit] where ddate>='{0}' and ddate<='{1}' and dmode='4'", dtpbegin.Text, dtpend.Text + " 23:59:59");
            SqlConnection conn100 = new SqlConnection(constr);
            conn100.Open();
            SqlCommand comm100 = new SqlCommand(tk,conn100);
            txttkhj.Text = comm100.ExecuteScalar().ToString();
            conn100.Close();

            return ds;
        }
        public DataSet selectcardID()
        {
            DataSet ds = new DataSet();
            SqlConnection conn = new SqlConnection(constr);
            conn.Open();
            string sql =string.Format( "SELECT  [dmid] as 卡号 ,[dmoney] as 续费金额,[ddate] as 续费日期 ,[dremark] as 备注 ,[dmode]  as 方式  ,[czdh] as 单据号 ,[zsje] as 赠送金额 ,[jsr] as 经手人 ,[skzh] as 收款方式 ,[skje]  as 收款金额 FROM [deposit] where ddate>='{0}' and ddate<='{1}' and dmid='{2}'",dtpbegin.Text,dtpend.Text,txtcardID.Text + " 23:59:59");
            SqlDataAdapter sda = new SqlDataAdapter(sql,conn);
            DataTable dt = new DataTable();
            sda.Fill(dt);
            dataGridView1.DataSource = dt;
            string sql1= string.Format("SELECT  [dmid] ,[dmoney] ,[ddate] ,[dremark]  ,[dmode]  ,[czdh]  ,[zsje]  ,[jsr] ,[skzh]  ,[skje] FROM [deposit] where ddate>='{0}' and ddate<='{1}' and dmid='{2}'", dtpbegin.Text, dtpend.Text, txtcardID.Text + " 23:59:59");
            SqlDataAdapter sda1 = new SqlDataAdapter(sql1,conn);
            sda1.Fill(ds, "deposit");

            conn.Close();






            return ds;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            DevExpress.XtraReports.UI.XtraReport xr = new DevExpress.XtraReports.UI.XtraReport();
            xr.LoadLayout(AppDomain.CurrentDomain.BaseDirectory + @"bb\会员充值表.repx");
            if (txtcardID.Text==string.Empty)
            {
                xr.DataSource = selecttime();
            }
            else
            {
                xr.DataSource = selectcardID();
            }
            
             xr.ShowPreview();
            //xr.Print();
        }

        private void frmhyczmx_Load(object sender, EventArgs e)
        {

        }

        private void dataGridView1_RowPostPaint(object sender, DataGridViewRowPostPaintEventArgs e)
        {
            Rectangle rectangle = new Rectangle(e.RowBounds.Location.X,
       e.RowBounds.Location.Y,
       dataGridView1.RowHeadersWidth - 4,
       e.RowBounds.Height);

            TextRenderer.DrawText(e.Graphics, (e.RowIndex + 1).ToString(),
            dataGridView1.RowHeadersDefaultCellStyle.Font,
                rectangle,
                dataGridView1.RowHeadersDefaultCellStyle.ForeColor,
                TextFormatFlags.VerticalCenter | TextFormatFlags.Right);
        }
    }
}
